﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

/*
 * Application Name:Fast Copy Text Tool
 * 
 * Email:chengshongguo@sina.com
 * 
 * Host:elwy.info
 * 
 * Last Update Time:04-17-2014
 * 
 * Last Update Log:
 *  1.Add Lock Button Function;
 *  2.
 */
namespace copy
{
    public partial class mainFrm : Form
    {
        public mainFrm()
        {
            InitializeComponent();
        }
        Timer t_More_Option = new Timer();
        Timer t_FormTop = new Timer();
        bool MoreOptionOpen = false;
        Point p = new Point(683, 342);
        Point p1 = new Point(683, 257);
        private int choiseNoticeListId = -1;
        private NoticeList noticeList = null;
        private List<Button> buttons = new List<Button>(); 
        private void Form1_Load(object sender, EventArgs e)
        {
            InitializationDB();
            bool k = true;
            choiseCopyExamples c = new choiseCopyExamples(dbPath);
            c.Owner = this;
            if (c.ShowDialog() == DialogResult.OK)
            {
                if (c.noticelistId != -1)
                {
                    choiseNoticeListId = c.noticelistId;
                }
                else
                {
                    k = false;
                }
                if (c.noticeList_F != null)
                {
                    this.noticeList = c.noticeList_F;
                }
                else
                {
                    MessageBox.Show("Unauthorized access to the interface, the program will exit");
                    k = false;
                }
            }
            else
            {
                k = false;
                MessageBox.Show("Unauthorized access to the interface, the program will exit");
            }
            if (k)
            {
                #region - buttonIni -
                label1.Tag = button1;
                label2.Tag = button2;
                label3.Tag = button3;
                label4.Tag = button4;
                label5.Tag = button5;
                label6.Tag = button6;
                label7.Tag = button7;
                label8.Tag = button8;
                label9.Tag = button9;
                label10.Tag = button10;
                label11.Tag = button11;
                label12.Tag = button12;
                label13.Tag = button13;
                label14.Tag = button14;
                label15.Tag = button15;
                label16.Tag = button16;
                buttons.AddRange(new BindingList<Button>()
                {
                    button1,
                    button2,
                    button3,
                    button4,
                    button5,
                    button6,
                    button7,
                    button8,
                    button9,
                    button10,
                    button11,
                    button12,
                    button13,
                    button14,
                    button15,
                    button16,
                });
                #endregion
                t_More_Option.Interval = 10;
                t_More_Option.Tick += new EventHandler(t_More_Option_Tick);
                t_FormTop.Interval = 1;
                t_FormTop.Tick += new EventHandler(t_FormTop_Tick);

                db = new DBHelper(dbPath);
                this.Text = this.Text + " [" + noticeList.Name + "]";
                List<OpTable> ot = db.SelectOpTable();
                if (ot != null)
                {
                    foreach (OpTable opTable in ot)
                    {
                        switch (opTable.Name)
                        {
                            case "MoreOption":
                                MoreOptionOpen = opTable.Value.ToUpper() != "TRUE";
                                t_More_Option.Start();
                                break;
                            case "AllTop":
                                AllTop.Checked = opTable.Value.ToUpper() == "TRUE" ? true : false;
                                break;
                            case "locking Button":
                                cbLkBtn.Checked = opTable.Value.ToUpper() == "TRUE" ? true : false;
                                break;
                            case "Opacity":
                                for (int i = 0; i < cbOpacity.Items.Count; i++)
                                {
                                    string str = cbOpacity.Items[i].ToString();
                                    if (cbOpacity.Items[i].ToString() == opTable.Value)
                                    {
                                        cbOpacity.SelectedIndex = i;
                                        break;
                                    }
                                }
                                break;
                            case "CopyRight":
                                txtCopyRight.Text = opTable.Value.ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Sorry,'save.db' File is damaged ,We will try to fix");
                    InitializationDB();
                    MessageBox.Show("Successful!");
                }
                //获取数据
                List<copyNotice> copyNotices = db.SelectnoticeTable(noticeList.Id);
                if (copyNotices != null)
                {

                    int w = copyNotices.Count, r = buttons.Count;
                    for (int i = 0; i < w; i++)
                    {
                        if (i >= r) break;
                        else
                        {
                            buttons[i].Tag = copyNotices[i];
                        }
                    }
                    lableGetNotice();
                }
            }
            else
            {
                Close();
            }
        }

        void t_FormTop_Tick(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        void t_More_Option_Tick(object sender, EventArgs e)
        {
            if (MoreOptionOpen)
            {
                if (this.Height >= p1.Y)
                    this.Height -= 5;
                else
                {
                    MoreOptionOpen = false;
                    t_More_Option.Stop();
                }
                

            }
            else
            {
                if (this.Height <= p.Y)
                    this.Height += 5;
                else
                {
                    MoreOptionOpen = true;
                    t_More_Option.Stop();
                }
                   
            }
           
        }

        private void btnClick(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Windows.Forms.Button")
            {
                Button l = (Button)sender;
                if (l.Tag== null)
                {
                    if (!cbLkBtn.Checked)
                    {
                        addForm af = new addForm();
                        af.Owner = this;
                        if (AllTop.Checked)
                        {
                            t_FormTop.Stop();
                            this.TopMost = false;
                        }
                        if (af.ShowDialog() == DialogResult.OK)
                        {
                            copyNotice c = af.Copynotice;
                            l.Tag = c;
                            lableGetNotice();
                        }
                        if (AllTop.Checked)
                        {
                            t_FormTop.Start();
                        }
                    }
                }
                else
                {
                    copyNotice c = (copyNotice) l.Tag;
                    if (!string.IsNullOrEmpty(c.Text))
                        Clipboard.SetText(c.Text);
                    l.BackColor = Color.Red;
                    lblState.Text = c.Notice;
                }
            }
            else if (sender.GetType().ToString() == "System.Windows.Forms.Label")
            {
                Label l = (Label) sender;
                Button b = (Button) l.Tag;
                b.PerformClick();
            }
        }

        private void lableGetNotice()
        {
            foreach (Control item in this.Controls)
            {
                if (item.GetType().ToString() == "System.Windows.Forms.Label")
                {
                    Label l = (Label) item;
                    if (l.Tag != null)
                    {
                        Button b = (Button)l.Tag;
                        if (b.Tag != null)
                        {
                            l.Text = ((copyNotice)b.Tag).Notice;
                        }
                    }
                }
            }
        }

        //添加一个锁定功能
        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item.GetType().ToString() == "System.Windows.Forms.Button")
                {
                    Button b = (Button)item;
                    b.BackColor = this.BackColor;
                    lblState.Text = "";
                }
            }
        }

        private void btnCopyRight_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(txtCopyRight.Text == string.Empty ? "" : txtCopyRight.Text);
        }

        private void btnMore_Click(object sender, EventArgs e)
        {
            t_More_Option.Start();
        }



        private void cbOpacity_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = cbOpacity.Text;
            str=str.Replace("%", " ");
            this.Opacity = Convert.ToDouble(str) / 100;
        }

        private void AllTop_CheckedChanged(object sender, EventArgs e)
        {
            if (AllTop.Checked)
            {
                t_FormTop.Start();
            }
            else
            {
                t_FormTop.Stop();
                this.TopMost = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!File.Exists(dbPath))
            {
                InitializationDB();
            }
            db.UpdateOpTable("MoreOption", MoreOptionOpen.ToString());
            db.UpdateOpTable("locking Button", cbLkBtn.Checked.ToString());
            db.UpdateOpTable("AllTop", AllTop.Checked.ToString());
            db.UpdateOpTable("Opacity",cbOpacity.SelectedIndex !=-1 ?cbOpacity.Text:"100");
            db.UpdateOpTable("CopyRight", txtCopyRight.Text);
            foreach (Button button in buttons)
            {
                if(button.Tag!=null)
                    saveNotice((copyNotice)button.Tag);
            }
            //saveNotice((copyNotice)button1.Tag);
        }

        private void saveNotice(copyNotice cn)
        {
            if (cn.Id == -1&&cn.NoticelistId==-1)
            {
                cn.NoticelistId = this.noticeList.Id;
                cn.Id=db.InsertnoticeTable(cn);
            }
            else
            {
                db.UpdatenoticeTable(cn);
            }
        }

        /// <summary>
        /// iniDBFile and Table
        /// </summary>
        private void InitializationDB()
        {
            //DBHelper dh = new DBHelper("C:\\1.b");
            if (!File.Exists(dbPath))
            {
                DBHelper.CreateDB(dbPath);
            }
            DBHelper db = new DBHelper(dbPath);
            db.InsertOpTable("MoreOption", "false");
            db.InsertOpTable("AllTop", "false");
            db.InsertOpTable("locking Button", "false");
            db.InsertOpTable("Opacity", "100");
            db.InsertOpTable("CopyRight", "");
        }

        private DBHelper db = null;
        private string dbPath = Application.StartupPath + "\\save.db";

        private void mainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (choiseNoticeListId != -1)
            {
                DialogResult dr =
                        MessageBox.Show("If you do not save too,you will lose all changes,\r\nAre you sure you want to quit?", "Exit?", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }

        }


    }
}
